package com.example.myproject.web;

import com.example.myproject.redis.redission.service.RedLockService;
import io.swagger.annotations.ApiOperation;
import org.redisson.RedissonMultiLock;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redLock")
public class RedLockController {
    private Logger logger = LoggerFactory.getLogger(RedLockController.class);

    @Autowired
    private RedLockService redLockService;

    //测试2分钟之后等不到是不是返回false了
    //1 测试下是否在多个节点都会产生key
    //2 测试下A节点没执行完，b节点是否会等待
    @ApiOperation(value="multiTask", notes="multiTask")
    @RequestMapping(value="/multiMaster", method= RequestMethod.GET)
    public String getMultiNodeTask(String uuid){

        logger.info("multiMaster---");
        RLock rLock =  redLockService.tryLock(uuid,60,500);
        try {
            Thread.sleep(2*60*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(rLock!=null){
            rLock.unlock();
        }
        logger.info("multiMaster,end");
        return  "ok";
    }


    //测试2分钟之后等不到是不是返回false了
    @ApiOperation(value="multiTask2", notes="multiTask")
    @RequestMapping(value="/multiMaster2", method= RequestMethod.GET)
    public String multiMaster2(String uuid){

        logger.info("multiMaster2---");
        RLock rLock =  redLockService.tryLock(uuid,60,300);
        logger.info("multiMaster2!=null");
        if(rLock != null){
            rLock.unlock();//这里会把线程1的给释放吗？不会释放不属于当前线程的锁呢。
        }
        logger.info("multiMaster2---end");
        return  "ok";
    }
}
